package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**

 */
@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {

    @Override
    public void importOrderSettings(List<String[]> orderSettingList) {
        List<OrderSetting> orderSettingImportList = changeStringArrayToList(orderSettingList);
        //循环查看日期是否存在  如果存在,就更新    不存在,就添加
        for (OrderSetting orderSetting : orderSettingImportList) {
            QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<OrderSetting>();
            queryWrapper.eq("orderdate", orderSetting.getOrderDate());
            OrderSetting orderSettingIsOrNotExist = baseMapper.selectOne(queryWrapper);
            if (orderSettingIsOrNotExist != null) {
                orderSetting.setId(orderSettingIsOrNotExist.getId());
            }
        }
        saveOrUpdateBatch(orderSettingImportList);
    }

    @Override
    public Map findSettingData(int year, int month) {
        String beginDate = year + "-" + month + "-1";
        String endDate = year + "-" + month + "-31";
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("orderdate", beginDate, endDate);
        List<OrderSetting> orderSettings = baseMapper.selectList(queryWrapper);
        HashMap<String, OrderSetting> map = new HashMap<>();
        for (OrderSetting orderSetting : orderSettings) {
            Date orderDate = orderSetting.getOrderDate();
            String dateString = DateUtils.parseDate2String(orderDate, "yyyy-MM-dd");
            map.put(dateString, orderSetting);
        }
        return map;
    }

    @Override
    @LcnTransaction
    public Boolean updateNumberByOrderdate(int number, String orderdate) {
        return baseMapper.updateNumberByOrderdate(number, orderdate);
    }

    @Override
    public int isOrderOk(String orderDate) {
        return baseMapper.isOrderOk(orderDate);
    }

    @Override
    public void updateReservationByOrderDate(String orderDate) {
        baseMapper.updateReservationByOrderDate(orderDate);
    }

    private List<OrderSetting> changeStringArrayToList(List<String[]> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() != 0) {
            List<OrderSetting> orderSettings = new ArrayList<>();
            for (String[] strings : orderSettingList) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setReservations(0);
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                orderSetting.setOrderDate(DateUtils.parseString2Date(strings[0], "yyyy/MM/dd"));
                orderSettings.add(orderSetting);
            }
            return orderSettings;
        }
        return null;
    }
}
